package net.xo68.destiny.contract.domain;

import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import org.apache.commons.lang3.StringUtils;

/**
 * 响应结果
 * @author wuxie
 * @version 2020-10-25
 */
@ApiModel("响应结果")
public class RespondResult<T> extends RespondEntity {

    public static <B> RespondResult<B> ok(){
        return ok(SUCCESS_MESSAGE,null);
    }

    public static <B> RespondResult<B> ok(B body){
        return ok(RespondEntity.SUCCESS_MESSAGE,body);
    }
    public static <B> RespondResult<B> ok(String message,B body){
        RespondResult<B> respondResult=new RespondResult<>();
        respondResult.setState(SUCCESS);
        respondResult.setMessage(message);
        respondResult.setBody(body);
        return respondResult;
    }

    public static <B> RespondResult<B> fail(){
        return fail(FAIL_MESSAGE,null);
    }

    public static <B> RespondResult<B> fail(B body){
        return fail(RespondEntity.FAIL_MESSAGE,body);
    }

    public static <B> RespondResult<B> fail(String message,B b){
        RespondResult<B> respondResult=new RespondResult<>();
        respondResult.setState(FAIL);
        respondResult.setMessage(message);
        respondResult.setBody(b);
        return respondResult;
    }

    /**
     * 响应结果构建者
     */
    public static class Builder{

        private int state;
        private String message;

        public static Builder builer(){
            return new Builder();
        }

        public Builder state(int state){
            this.state=state;
            return this;
        }
        public Builder message(String message){
            this.message=message;
            return this;
        }
        public <T> RespondResult<T> build(T body){
            RespondResult<T> respondResult=new RespondResult<>();
            respondResult.setState(this.state);
            if(StringUtils.isNotEmpty(this.message)){
                respondResult.setMessage(this.message);
            }else {
                respondResult.setMessage(StringUtils.EMPTY);
            }
            respondResult.setBody(body);
            return respondResult;
        }
    }


    /**
     * 响应正文
     */
    @ApiModelProperty("响应正文")
    private T body;


    public T getBody() {
        return body;
    }

    public void setBody(T body) {
        this.body = body;
    }

    @Override
    public String toString() {
        return "RespondResult{" +
                "state=" + this.getState() +
                "message=" + this.getMessage() +
                "body=" + body +
                '}';
    }
}
